package MyArrayList;

import java.util.Arrays;

public class MyArrayList<T> {
    private T[] data;
    private int size=0;

    public MyArrayList(int num){
        data = (T[])new Object[num];
    }
    public MyArrayList(){
        this(10);
    }

    public void add(T t){
        if (size<data.length){
            data[size++]=t;
        }else {
            data= Arrays.copyOf(data,size*2);
            data[size++]=t;
        }
    }

    public int size(){
        return size;
    }
    public int length(){
        return data.length;
    }


    public T get(int index){
        if (index>0&&index<size) {
            return data[index];}
        return null;
    }

    public void set(int index,T t){
        if (index>0&&index<size){
            data[index] = t;
        }
    }

    public String toString(){
        StringBuilder stringBuilder = new StringBuilder("[");
        stringBuilder.append(data[0]);
        for(int i=1;i<data.length;i++){
            if(data[i]==null) break;
            stringBuilder.append(","+data[i]);
        }
        stringBuilder.append("]");
        return stringBuilder.toString();

    }

}
